package leetcode_题库;

import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2022/8/20 12:54
 */
public class _566_重塑矩阵 {
    public static void main(String[] args) {
        int[][] mat = new int[][]{{1, 2}, {3, 4}};
        int r = 1;
        int c = 4;

        System.err.println(Arrays.deepToString(matrixReshape(mat, r, c)));

    }

    public static int[][] matrixReshape(int[][] mat, int r, int c) {
        //判断是否符合变形要求，不符合就返回原数组
        if (mat.length == 0 || mat[0].length * mat.length != r * c) {
            return mat;
        }
        //定义新二维数组
        int[][] ret = new int[r][c];
        int rr = 0;
        int cc = 0;
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                //让第rr-cc个元素赋值为i-j
                ret[rr][cc] = mat[i][j];
                //二级数组下标++
                cc++;
                //倘若二级数组满载，则归0，且让一级数组右移一位
                if (cc == c) {
                    cc = 0;
                    rr++;
                }
            }
        }
        return ret;
        /*
            【mine】
            > 2022/08/20 13:07:36
            解答成功:
                执行耗时:0 ms,击败了100.00% 的Java用户
                内存消耗:42.3 MB,击败了61.93% 的Java用户

         */
    }
}
